package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.RepairMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Repair;
import com.itheima.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RepairServiceImpl implements RepairService {

        @Autowired
        private RepairMapper repairMapper;

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        //获取总记录数：
//        Long count=repairMapper.count();
//        //按分页查询结果列表
//        Integer start=(page-1)*pageSize;
//       List<Repair> repairList= repairMapper.page(start,pageSize);
//        //封装pagebean对象
//        PageBean pageBean=new PageBean(count,repairList);
//        return pageBean;
//    }

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);

        //执行查询
        List<Repair> repairList=repairMapper.list();
        Page<Repair> r=(Page<Repair>) repairList;

        //封装pagebean对象
        PageBean pageBean=new PageBean(r.getTotal(),r.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        repairMapper.delete(ids);
    }

    @Override
    public void save(Repair repair) {
        repair.setCreateTime(LocalDateTime.now());
        repair.setUpdateTime(LocalDateTime.now());
        repairMapper.insert(repair);
    }

    @Override
    public Repair getById(Integer id) {
        return repairMapper.getById(id);
    }

    @Override
    public void update(Repair repair) {
        repair.setUpdateTime(LocalDateTime.now());
        repairMapper.update(repair);
    }
}
